package com.literature.search.user.service.impl;

import com.literature.search.core.common.MessageConstant;
import com.literature.search.core.entity.R.AjaxJson;
import com.literature.search.core.entity.Role;
import com.literature.search.user.service.IUserAuthService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @Author: DangKong
 * @Date: 2023/3/2 00:27
 * @Description:
 */

@Service
public class UserAuthServiceImpl implements IUserAuthService {

    public static final String ROLE_NAME= "role_name"; // 角色名
    private static final String COLLECTION_NAME = "role"; // 集合名称: role
    @Resource
    MongoTemplate mongoTemplate;



    @Override
    public AjaxJson addRole(Role role) {
        role.setCreateTime(new Date());
        // 检查roleName是否存在
        Query query = new Query(Criteria.where(ROLE_NAME).is(role.getRoleName()));
        Role dbRole = mongoTemplate.findOne(query, Role.class, COLLECTION_NAME);
        if (dbRole != null) {
            return AjaxJson.getError(MessageConstant.ROLE_EXIST);
        }
        mongoTemplate.save(role, COLLECTION_NAME);
        return AjaxJson.getSuccess(MessageConstant.SUCCESS);
    }

}
